package net.myspring.common.utils;

import net.myspring.common.domain.HttpClientEntity;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by liuj on 2016/10/2.
 */
public class HttpClientUtils {
    private static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);

    private static HttpClientContext clientContext;

    static {
        clientContext=HttpClientContext.create();
    }

    public static String doHttpGet(HttpClientEntity httpClientEntity) {
        String result = "";
        CloseableHttpClient httpclient = getHttpClient(httpClientEntity);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(httpClientEntity.getConnectTimeout()).build();
        CloseableHttpResponse response = null;
        try {
            HttpGet httpGet = new HttpGet(httpClientEntity.getHttpUrl());
            httpGet.setConfig(requestConfig);
            response = httpclient.execute(httpGet);
            result = EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            logger.error("httpclient request error:" + e.getMessage());
        } finally {
            try {
                httpclient.close();
                if (response != null) {
                    response.close();
                }
            } catch (Exception e) {
                logger.error("httpclient close error:" + e.getMessage());
            }
        }
        return result;
    }

    public static String doHttpPost(HttpClientEntity httpClientEntity) {
        String result = "";
        CloseableHttpClient httpclient = getHttpClient(httpClientEntity);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(httpClientEntity.getConnectTimeout()).build();
        CloseableHttpResponse response = null;
        try {
            HttpPost httpPost = new HttpPost(httpClientEntity.getHttpUrl());
            httpPost.setConfig(requestConfig);
            if (httpClientEntity.getUrlEncodedFormEntity() != null) {
                httpPost.setEntity(httpClientEntity.getUrlEncodedFormEntity());
            }
            response = httpclient.execute(httpPost);
            result = EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            logger.error("httpclient request error:" + e.getMessage());
        } finally {
            try {
                httpclient.close();
                if (response != null) {
                    response.close();
                }
            } catch (Exception e) {
                logger.error("httpclient close error:" + e.getMessage());
            }
        }
        return result;
    }

    public static CloseableHttpClient getHttpClient(HttpClientEntity httpClientEntity) {
        CookieStore cookieStore = new BasicCookieStore();
        List<BasicClientCookie> basicClientCookies = httpClientEntity.getBasicClientCookies();
        if (Collections3.isNotEmpty(basicClientCookies)) {
            basicClientCookies.forEach(cookieStore::addCookie);
        }
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCookieStore(cookieStore)
                .setUserAgent(httpClientEntity.getUserAgent())
                .build();
        return httpclient;
    }
}
